package com.igeekhome.ccs.biz.Impl;

import com.igeekhome.ccs.biz.ICustomerServiceBiz;
import com.igeekhome.ccs.entity.CustomerService;
import com.igeekhome.ccs.entity.CustomerServiceTime;
import com.igeekhome.ccs.mapper.CustomerServiceGroupMapper;
import com.igeekhome.ccs.mapper.CustomerServiceMapper;
import com.igeekhome.ccs.mapper.CustomerServiceRoleMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CustomerServiceBizImpl implements ICustomerServiceBiz {
    @Autowired
    private CustomerServiceMapper mapper;
    @Autowired
    private CustomerServiceGroupMapper groupMapper;
    @Autowired
    private CustomerServiceRoleMapper roleMapper;

//    客服更新自己的信息,已测试
    @Override
    public int update(CustomerService cs) {
        int a;
        if(getOneByUsername(cs.getUsername())!=null){
            System.out.println(cs.getCsId());
            if(getOneById(cs.getCsId())==null){
                a = mapper.update(cs);
            }else {
                System.out.println("客服工号已存在，修改失败");
                a=0;
            }

        }else{
            System.out.println("用户名已存在，修改失败");
            a=0;
        }
        return a;
    }
//  根据用户名获得cs对象,已测试
    @Override
    public CustomerService getOneByUsername(String username) {
        CustomerService cs = mapper.getOneByUsername(username);
        return cs;
    }
//  根据id获得cs对象,已测试
    @Override
    public CustomerService getOneById(int id) {
        CustomerService cs = mapper.getOneById(id);
        return cs;
    }

    @Override
    public List<CustomerServiceTime> getWorkAll() {
        List<CustomerServiceTime> list = mapper.getWorkAll();
        return list;
    }
    /**
     * 根据组获得 客服时间记录
     * @return
     */
    @Override
    public List<CustomerServiceTime> queryWorkByGroup(String group) {
        return mapper.queryWorkByGroup(group);
    }

    /**
     * 根据客服 realName获得 客服时间记录
     * @return
     */
    @Override
    public List<CustomerServiceTime> queryWorkByCs(String cs) {
        return mapper.queryWorkByCs(cs);
    }

    /**
     * 根据起止时间获得客服时间记录
     * @return
     */
    @Override
    public List<CustomerServiceTime> queryWorkByTime(Date start, Date end) {
        return mapper.queryWorkByTime(start, end);
    }

//👇梁仲轩----------------------------

    /**
     * 登录
     * 参数：登录名,密码
     */
    @Override
    public int login(String  username, String password) {
       try
       {
           CustomerService cs = new CustomerService();
           cs.setUsername(username);
           cs.setPassword(password);
           mapper.login(cs);
       }
       catch (Exception e){
           System.out.println("用户名不存在或密码不正确");
       }
        return 0;
    }
    /**
     * 注册
     * 参数：登录名,密码
     */

    @Override
    public int regist(String  username, String password) {
        try
        {
            CustomerService cs = new CustomerService();
            cs.setUsername(username);
            cs.setPassword(password);
            mapper.regist(cs);
        }
        catch (Exception e) {
            System.out.println("用户名已存在");
            return -1;
        }
        return 0;
    }



    @Override
    public int getOnlineNumber() {
        return mapper.getOnlineNumber();
    }

    @Override
    public List<Map<String, Object>> getCsInfolist() {

        return mapper.getCsInfolist();
    }
//👆梁仲轩-----------------------------


//👇张煜涵，有事找我----------------------------
    //拿到所有客服信息
    @Override
    public List<CustomerService> query() {
        return mapper.query();
    }

        //客服信息做模糊搜索
    @Override
    public List<CustomerService> matchCsInfowithString(String str) {
        List<CustomerService> totalList = mapper.query();
        List<CustomerService> resultList = new ArrayList<CustomerService>();
        Iterator<CustomerService> iter = totalList.iterator();
        str=str.trim();
        for(int i = 0 ; i<totalList.size(); i++) {
    //           模糊匹配、大小写均可
            Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
            Matcher matcher1 = pattern.matcher(totalList.get(i).getNickName());
            Matcher matcher2 = pattern.matcher(totalList.get(i).getRealName());
            Matcher matcher3 = pattern.matcher(totalList.get(i).getPhone());
            String strId = totalList.get(i).getCsId() + "";
            Matcher matcher4 = pattern.matcher(strId);
            if (matcher1.find() || matcher2.find() || matcher3.find() || matcher4.find()) {
                resultList.add(totalList.get(i));
            }
        }
        return resultList;
    }
    //修改客服工作状态
    @Override
    public void changeWorkState(int csId ,String oldWorkStatus) {
        oldWorkStatus = oldWorkStatus.trim();
        String newWorkStatus;
        if(oldWorkStatus.equals("停用")){
            newWorkStatus ="开启";
        }else{
            newWorkStatus ="停用";
        }
        mapper.updateWorkState(csId,newWorkStatus);
    }

    //用id删除单个客服
    @Override
    public void deleteCsById(int csId) {
        CustomerService customerService = new CustomerService();
        customerService = mapper.getOneById(csId);
        if(customerService.getGroupName()!= null ){
            int oldNumber = groupMapper.getNumberByCsgId(customerService.getGroupId());
            int newNumber = oldNumber-1;
            groupMapper.decreaseNumber(customerService.getGroupId(),newNumber);
        }
        if(customerService.getRoleName()!= null ){
            int oldNumber = roleMapper.getNumberByCsrId(customerService.getGroupId());
            int newNumber = oldNumber-1;
            roleMapper.decreaseNumber(customerService.getRoleId(),newNumber);
        }
        mapper.deleteCsById(csId);
    }

    //用list(id)批量删除客服
    @Override
    public void deleteCsByList(List<Integer> list) {
        for(int i = 0 ;i<list.size();i++) {
            this.deleteCsById(list.get(i));
        }
    }


    //新增客服
    @Override
    public void createNewCs(CustomerService customerService) {
        if(customerService.getGroupName()!= null ){
            int oldNumber = groupMapper.getNumberByCsgId(customerService.getGroupId());
            int newNumber = oldNumber+1;
            groupMapper.increaseNumber(customerService.getGroupId(),newNumber);
        }
        if(customerService.getRoleName()!= null ){
            int oldNumber = roleMapper.getNumberByCsrId(customerService.getRoleId());
            int newNumber = oldNumber+1;
            roleMapper.increaseNumber(customerService.getRoleId(),newNumber);
        }
        mapper.createNewCs(customerService);
    }

    //编辑客服
    @Override
    public int updateCs(CustomerService customerService) {
        this.deleteCsById(customerService.getCsId());
        this.createNewCs(customerService);
        return 0;
    }

//👆张煜涵，有事找我----------------------------
}
